﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Admin/AdminSite.Master" AutoEventWireup="true"
    CodeBehind="CategoryList.aspx.cs" Inherits="phudieuhungthinh.Admin.CategoryList" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <link href="/Styles/control-style.css" rel="stylesheet" type="text/css" />
    <script src="/Scripts/vm/handler.js" type="text/javascript"></script>
    <style type="text/css">
        .btn-add
        {
            background-color: #ccc;
            color: black;
            font-family: 'Oswald';
            font-size: 20px;
            text-decoration: none;
            cursor: pointer;
            border: none;
        }
    </style>
    <script type="text/javascript">

        var dataHandler = new handler(location.pathname);
        function addCategoryClick() {
            window.location.href = '/Admin/CategoryItem.aspx';
        }
        function deleteCategoryClick(id) {
            var answer = confirm("Bạn có muốn xóa chuyên mục?");
            if (answer) {
                $.ajax({
                    type: "POST",
                    url: location.pathname + "/GetProductCategories",
                    data: JSON.stringify({ categoryId: id }),
                    contentType: "application/json",
                    success: function (data) {
                        var isDelete;
                        if (data.d == null) {
                            isDelete = true;
                        }
                        else {
                            var reanswer = confirm("Chuyên mục đã có sản phẩm. Bạn có muốn xóa không?");
                            isDelete = reanswer;
                        }
                        if (isDelete) {
                            $.ajax({
                                type: "POST",
                                url: location.pathname + "/DeleteCategory",
                                data: JSON.stringify({ categoryId: id }),
                                contentType: "application/json",
                                success: function () {
                                    window.location.href = location.pathname;
                                },
                                error: function () {
                                    alert("Fail");
                                }
                            });
                        }
                    },
                    error: function () {
                        alert("Fail");
                    }
                });
            }
        }
        $(document).ready(function () {
            //Prepare jtable plugin
            $('#CategoryTableContainer').jtable({
                title: 'Danh sách chuyên mục',
                paging: true, //Enables paging
                pageSize: 10, //Actually this is not needed since default value is 10.
                sorting: true, //Enables sorting
                defaultSorting: 'Name ASC', //Optional. Default sorting on first load.
                actions: {
                    listAction: location.pathname + '/GetCategoryList'
                },
                fields: {
                    ID: {
                        key: true,
                        create: false,
                        edit: false,
                        list: false
                    },
                    Name: {
                        title: 'Chuyên mục',
                        width: '50%'
                    },
                    CustomActionEdit: {
                        title: 'Sửa',
                        list: true,
                        sorting: false,
                        edit: false,
                        display: function (data) {
                            if (data.record) {
                                return '<a href="/Admin/CategoryItem.aspx?id=' + data.record.ID + '">Sửa</a>';
                            }
                            return null;
                        }
                    },
                    CustomActionDelete: {
                        title: 'Xóa',
                        list: true,
                        sorting: false,
                        edit: false,
                        display: function (data) {
                            if (data.record) {
                                return "<a href='javascript:void(0)' onclick=\"deleteCategoryClick('" + data.record.ID + "');\">Xóa</a>";
                            }
                            return null;
                        }
                    }
                }
            });
            //Load category list from server
            $('#CategoryTableContainer').jtable('load');
        }); 
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div class="button add">
        <a href="javascript:void(0)" onclick="addCategoryClick()">Thêm chuyên mục</a>
    </div>
    <div class="clear">
    </div>
    <div id="CategoryTableContainer">
    </div>
</asp:Content>
